package com.bop.dehong.loan;

import com.base.dehong.base.AdapterBase;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.DeCaifuActivity;
import com.bop.dehong.info.TenderingInfo;
import com.bop.dehong.info.TenderingListInfo;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * @author TangWenhan/YeFengyun
 *2015年11月25日下午1:52:26
 */
public abstract class LoanBaseFragment extends BaseFragment implements OnHeaderRefreshListener,OnFooterRefreshListener{
	public PullToRefreshView mRefreshView;
	public ListView mListView;
	public LinearLayout mEmptyLayout;
	public final int REQ_REFRESH = 0;
	public final int REQ_LOAD  = 1;
	public int REQ_TYPE = 0;

	public int mCurrentPage = 1;
	public int mTotalPage = 0;
	AdapterBase mAdapter;

	@Override
	protected void initTop() {
	}

	@Override
	protected void initUI() {
		mListView = getView(R.id.listView1);
		mListView.setOnItemClickListener(this);
		mRefreshView = getView(R.id.pullToRefreshView1);
		mEmptyLayout = getView(R.id.empty_layout);
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterRefreshListener(this);
	}
	public abstract void reqData();

	//	public abstract void initData(Object info);

	public abstract AdapterBase<?> getAdapter() ;

	@SuppressWarnings("unchecked")
	public void initData(Object info) {

		TenderingListInfo tInfo = (TenderingListInfo) info;
		if (REQ_TYPE == REQ_REFRESH) {
			mAdapter.clear();
		}
		mTotalPage = tInfo.totalpages;
		if (tInfo.list != null && tInfo.list.size() > 0) {
			mAdapter.appendToList(tInfo.list);
			mEmptyLayout.setVisibility(View.GONE);
		}else {
			mEmptyLayout.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void initData() {
		if (mAdapter == null) {
			mAdapter = getAdapter();
		}
		mListView.setAdapter(mAdapter);
		if (mAdapter.size() <= 0) {
			reqData();
		}
	}

	@Override
	protected int getContentViewId() {
		return R.layout.frag_my_tendering;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_LOAD;
		if (mCurrentPage  < mTotalPage) {
			mCurrentPage++;
			reqData();
		}else {
			mRefreshView.onRefreshComplete();
			ToastUtils.show(getActivity(), "没有数据可加载");
		}
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mRefreshView.onRefreshComplete();
		REQ_TYPE = REQ_REFRESH;
		mCurrentPage = 1 ;
		reqData();
	}

	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		TenderingInfo tInfo = (TenderingInfo) mAdapter.get(position);
		Bundle bundle = new Bundle();
		bundle.putString(Config.CAT_ID, tInfo.id);
		openActivity(DeCaifuActivity.class, bundle);	
	}
}

